<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of profile
 *
 * @author Administrator
 */
class profile extends CI_Controller {

  //put your code here

  public function __construct() {
    parent::__construct();
  }

  public function index() {
    if (!userdata('provider_id')) {
      redirect('provider');
    }
    $user = $this->user_model->get(userdata('provider_user_id'));
    if ($user) {
      $provider = $this->provider_user_model->get(userdata('provider_id'));
      if ($provider) {
        $this->load->view('provider/profile', array('obj' => array_merge($user, $provider),
            'title_html' => $this->lang->line('label.provider.title.profile')));
        return;
      }
    }
    redirect('provider');
  }

  public function update() {
    $user = $_REQUEST;
    $group = $this->group_model->get(array('name' => 'Provider'));
    if (!empty($group)) {
      $user['group_id'] = $group['id'];
    } else
      $user['group_id'] = 0;
    $user['id'] = userdata('provider_user_id');
    $user = $this->user_model->create_obj($user);
    $provider = $_REQUEST;
    $provider['id'] = userdata('provider_id');
    $provider = $this->provider_user_model->create_obj($provider);
    $errors = array_merge($this->user_model->validate($user), $this->provider_user_model->validate($provider));
    if (!empty($errors))
      set_flash_error($errors);
    else {
      if (!empty($_REQUEST['password'])) {
        $user['password'] = $user['confirm_pass'] = md5($user['salt'] . md5($_REQUEST['password']));
      } else {
        unset($user['password']);
      }
      if (!empty($user['first_name']) || !empty($user['last_name'])) {
        $user['display_name'] = trim($user['first_name'] . ' ' . $user['last_name']);
      } else {
        $user['display_name'] = $user['username'];
      }
      $user_id = $this->user_model->save($user);
      if ($user_id) {
        $avatar = $this->base_lib->_upload_img('avatar', 'provider_user');
        if (!empty($avatar)) {
          $provider['avatar'] = $avatar;
        }
        $provider['date_update'] = time();
        $provider_id = $this->provider_user_model->save($provider);
        if ($provider_id) {
          set_userdata('provider_avatar', $provider['avatar']);
          set_flash_message($this->lang->line('message.provider.update'));
          redirect('provider/index');
          return;
        } else {
          set_flash_error($this->lang->line('error.faltal.error'));
        }
      } else
        set_flash_error($this->lang->line('error.faltal.error'));
    }
    $this->load->view('provider/profile', array('obj' => $_REQUEST, 'title_html' => $this->lang->line('label.provider.title.profile')));
  }

  public function active($active_code = '') {
    $user = $this->user_model->get(array('active_code' => $active_code));
    if (!empty($user)) {
      $provider = $this->provider_user_model->get(array('user_id' => $user['id']));
      if (!empty($provider)) {
        if ($user['is_active'] != 1 || $provider['status'] != PROFILE_STATUS_ACTIVE) {
          $this->_payment($active_code, $user, $provider);
        } else set_flash_message($this->lang->line('message.user.activated'));
      } else {
        set_flash_error($this->lang->line('error.provider.profile.not.found'));
      }
    } else {
      set_flash_error($this->lang->line('error.user.not.found'));
    }
    redirect('provider');
  }

  private function _payment($active_code, $user, $provider) {
    $amount = $this->setting_model->item('payment_provider_amount');
    if (!empty($amount) && $this->setting_model->item('paypal_enabled') == '1') {
      $currency = $this->setting_model->item('payment_currency');
      if (empty($currency))
        $currency = 'USD';
      $attrs = array(
          'cmd' => '_xclick',
          'business' => $this->setting_model->item('payment_account'),
          'return' => site_url('provider/profile/active_profile/' . $active_code . '/' . $user['id'] . '/' . $provider['id']),
          'cancel_return' => site_url('provider/profile/payment_fail'),
          'notify_url' => site_url('provider/profile/payment_fail'),
          'item_name' => $this->lang->line('payment.active.provider.profile', $title_payment),
          'currency_code' => $currency, 'amount' => $amount, 'rm' => '2'
      );
      if ($this->setting_model->item('paypal_env') == '1') {
        $paypal_url = 'https://www.paypal.com/cgi-bin/webscr';
      } else {
        $paypal_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
      }
      $params = array();
      foreach ($attrs as $key => $attr) {
        $params[] = $key . '=' . $attr;
      }
      $params = implode('&', $params);
      redirect($paypal_url . '?' . $params);
    } else {
      redirect('provider/profile/active_profile/'.$active_code.'/'.$user['id'] . '/' . $provider['id']);
    }
  }

  public function payment_fail() {
    set_flash_error($this->lang->line('error.payment.active.account.fail'));
    redirect('provider');
  }

  public function active_profile($active_code = '', $user_id = '', $provider_id = '') {
    if (!empty($active_code) && !empty($user_id) && !empty($provider_id)) {
      if (isset($_POST['mc_gross']) && isset($_POST['txn_id'])
              && isset($_POST['payment_fee']) && isset($_POST['mc_currency'])
              && isset($_POST['payer_id']) && isset($_POST['receiver_id'])) {
        $user = $this->user_model->get(array('id' => $user_id, 'active_code' => $active_code));
        if (!empty($user)) {
          $provider = $this->provider_user_model->get(array('id' => $provider_id, 'user_id' => $user['id']));
          if (!empty($provider)) {
            $user['is_active'] = true;
            $this->user_model->save($user);
            $provider['status'] = PROFILE_STATUS_ACTIVE;
            $provider['date_active'] = time();
            $provider['date_expire'] = strtotime('+' . $this->setting_model->item('payment_provider_active_date') . ' day');
            $this->provider_user_model->save($provider);
            /* log payment history */
            $this->payment_history_model->save(array(
                'amount' => $_POST['mc_gross'],
                'payment_date' => time(),
                'transaction_id' => $_POST['txn_id'],
                'payment_fee' => isset($_POST['payment_fee']) ? $_POST['payment_fee'] : 0,
                'mc_currency' => $_POST['mc_currency'],
                'payer_id' => $_POST['payer_id'],
                'receiver_id' => $_POST['receiver_id'],
                'provider_id' => $provider['id'],
                'end_user_id' => 0,
                'description' => $this->lang->line('label.payment.active.provider', array($provider['email'], date('s:i:H d/m/Y', time())))
            ));
            set_flash_message($this->lang->line('message.user.activated'));
            redirect('provider');
            return;
          }
        }
        set_flash_error($this->lang->line('error.provider.profile.not.found'));
      } else {
        set_flash_error($this->lang->line('error.payment.active.account.fatal.error'));
      }
    }
    redirect('provider');
  }

}